Passed
Pull Request — master (#2)
by Muhammad Dyas
01:24
created

CommandHandler.process   B

Complexity

Conditions 6

Size

Total Lines 33
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 28
dl 0
loc 33
rs 8.2746
c 0
b 0
f 0
1
import {NewPollFormCard} from '../cards/NewPollFormCard';
2
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1';
3
import {BaseHandler} from './BaseHandler';
4
import {buildOptionsFromMessage} from '../helpers/utils';
5
6
export class CommandHandler extends BaseHandler {
7
  private slashCommand: chatV1.Schema$SlashCommandMetadata | undefined;
8
9
  public constructor(event: chatV1.Schema$DeprecatedEvent) {
10
    super(event);
11
    this.parseSlashCommand();
12
13
    if (this.slashCommand === undefined) {
14
      throw new Error('No Slash Command found');
15
    }
16
  }
17
18
  parseSlashCommand() {
19
    this.getAnnotations().forEach((annotation) => {
20
      if (annotation.type === 'SLASH_COMMAND') {
21
        this.slashCommand = annotation.slashCommand!;
22
      }
23
    });
24
  }
25
26
  process(): chatV1.Schema$Message {
27
    console.log('command:', this.slashCommand!.commandName);
28
    switch (this.slashCommand!.commandName) {
29
      case '/poll':
30
        const argumentText = this.event.message?.argumentText?.trim() ?? '';
31
        const options = buildOptionsFromMessage(argumentText);
32
        return {
33
          actionResponse: {
34
            type: 'DIALOG',
35
            dialogAction: {
36
              dialog: {
37
                body: new NewPollFormCard(options).create(),
38
              },
39
            },
40
          },
41
        };
42
      default:
43
        return {
44
          thread: this.event.message!.thread,
45
          actionResponse: {
46
            type: 'NEW_MESSAGE',
47
          },
48
          text: 'Hi there! I can help you create polls to enhance collaboration and efficiency ' +
49
            'in decision-making using Google Chat™.\n' +
50
            '\n' +
51
            'Below is an example commands:\n' +
52
            '`/poll` - You will need to fill out the topic and answers in the form that will be displayed.\n' +
53
            '`/poll "Which is the best country to visit" "Indonesia"` - to create a poll with ' +
54
            '"Which is the best country to visit" as the topic and "Indonesia" as the answer\n' +
55
            '\n' +
56
            'We hope you find our service useful and please don\'t hesitate to contact us ' +
57
            'if you have any questions or concerns.',
58
        };
59
    }
60
  }
61
}
62